import 'dart:convert';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:taonile/config/config.dart';

import '../entity/user_info_entity.dart';
import '../http/api.dart';
import '../provider/userinfo_provider.dart';

///
/// 我得名片
///
class MineShareCard extends StatefulWidget {
  const MineShareCard({Key? key}) : super(key: key);

  @override
  State<MineShareCard> createState() => _MineShareCardState();
}

class _MineShareCardState extends State<MineShareCard> {
  late UserInfoEntity infoEntity;
  String qrData = '';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  getData() async {
    dynamic result = await Api.getMineCode();
    var jsonResult = jsonDecode(jsonEncode(result));
    if (jsonResult['code'] == 1) {
      setState(() {
        qrData = jsonResult['data'];
        LogUtil.v(qrData);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    infoEntity = Provider.of<UserInfoProvider>(context).userInfo;
    return Scaffold(
      backgroundColor: Config.colorBg,
      appBar: AppBar(
        title: const Text('分享名片'),
        centerTitle: true,
        backgroundColor: Config.colorBg,
      ),
      body: Container(
        width: double.infinity,
        color: Config.colorBg,
        height: double.infinity,
        child: Stack(
          children: [
            Image.asset(
              'assets/images/bg_mycard.png',
              width: double.infinity,
              height: 1500.w,
              fit: BoxFit.fill,
            ),
            Container(
              width: double.infinity,
              height: double.infinity,
              padding: EdgeInsets.symmetric(vertical: 100.w, horizontal: 38.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    alignment: Alignment.center,
                    width: double.infinity,
                    child: Container(
                      margin: EdgeInsets.only(top: 30.w),
                      width: 148.w,
                      height: 148.w,
                      // padding: EdgeInsets.all(0.2.w),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10.w),
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(10.w),
                        child: Image.asset(
                          'assets/images/launcher.png',
                          fit: BoxFit.fill,
                          width: double.infinity,
                          height: double.infinity,
                        ),
                      ),
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 30.w)),
                  Text(
                    "淘你乐生活链上商城",
                    style: TextStyle(color: Config.colorWhite, fontSize: 32.w),
                  ),
                  Padding(padding: EdgeInsets.only(top: 30.w)),
                  Container(
                    alignment: Alignment.center,
                    width: double.infinity,
                    child: Container(
                      margin: EdgeInsets.only(top: 200.w),
                      width: 460.w,
                      height: 460.w,
                      padding: EdgeInsets.all(5.w),
                      // padding: EdgeInsets.all(0.2.w),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Config.colorWhite,
                        borderRadius: BorderRadius.circular(12.w),
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(12.w),
                        child: QrImage(
                          data: qrData,
                          version: QrVersions.auto,
                          size: 450.w,
                        ),
                      ),
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 30.w)),
                  Text(
                    infoEntity.data.name,
                    style: TextStyle(color: Config.colorWhite, fontSize: 32.w),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
